package com.ziven.learn.dispatch;

import android.graphics.Outline;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;

import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.ziven.easygo.autowired.EasyGo;
import com.ziven.easygo.ui.AbstractBaseActivity;
import com.ziven.easygo.util.ResourceUtils;
import com.ziven.learn.LearnInterface;
import com.ziven.learn.R;
import com.ziven.processor.Learn;

/**
 * @author Ziven
 */
@Learn(learn = "圆角")
public class RoundCornerLearn implements LearnInterface {

    @Override
    public void learn() {
        EasyGo.easyGo(ResourceUtils.getContext(), RoundCornerActivity.class);
    }

    public static class RoundCornerActivity extends AbstractBaseActivity {

        @Override
        protected Object obtainLayout() {
            return R.layout.acitivity_round_corner;
        }

        @Override
        protected void initLayout() {

            int radius = this.getResources().getDimensionPixelSize(R.dimen.dp_24);

            //Outline
            getView(R.id.image_view_1).setClipToOutline(true);
            getView(R.id.image_view_1).setOutlineProvider(new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), radius);
                }
            });

            //GradientDrawable
            if(true) {
                //同时设置四个角
                GradientDrawable drawable = new GradientDrawable();
                drawable.setShape(GradientDrawable.RECTANGLE);
                drawable.setCornerRadius(radius);
                drawable.setColor(0xFFEEA2A4);
                ((ImageView) getView(R.id.image_view_4)).setImageDrawable(drawable);
            } else {
                //单独设置四个角
                GradientDrawable drawable = new GradientDrawable();
                drawable.setShape(GradientDrawable.RECTANGLE);
                drawable.setColor(0xFFEEA2A4);
                float[] radii = new float[]{
                        radius, radius,
                        0F, 0F,
                        0F, 0F,
                        radius, radius
                };
                drawable.setCornerRadii(radii);
                ((ImageView) getView(R.id.image_view_4)).setImageDrawable(drawable);
            }

            //RoundedBitmapDrawable
            RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(),
                    ResourceUtils.drawable2Bitmap(ResourceUtils.getDrawable(R.drawable.drawable_90_corner_2)));
            drawable.setCornerRadius(radius);
            ((ImageView) getView(R.id.image_view_4_2)).setImageDrawable(drawable);

            //Glide
            if(true) {
                //同时设置四个角
                Glide.with(this)
                        .asDrawable()
                        .load(R.drawable.drawable_90_corner)
                        .transform(new RoundedCorners(radius))
                        .into((ImageView) getView(R.id.image_view_5));
            } else {
                //单独设置四个角
                Glide.with(this)
                        .asDrawable()
                        .load(R.drawable.drawable_90_corner)
                        .transform(new GranularRoundedCorners(radius, radius, radius, radius))
                        .into((ImageView) getView(R.id.image_view_5));
            }
        }

        @Override
        protected void destroyLayout() {

        }
    }
}
